from haystack import indexes
from users.models import HouseListing

class HouseListingIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    id = indexes.IntegerField(model_attr='id')
    title = indexes.CharField(model_attr='title')
    address = indexes.CharField(model_attr='address')
    city = indexes.CharField(model_attr='city')
    description = indexes.CharField(model_attr='description')
    facilities = indexes.CharField(model_attr='facilities')
    price = indexes.CharField(model_attr='price')
    area = indexes.CharField(model_attr='area')
    orientation = indexes.CharField(model_attr='orientation', null=True, default='未知')
    deposit = indexes.CharField(model_attr='deposit', null=True, default='0')
    landlord = indexes.CharField(model_attr='landlord', null=True, default='未知')

    def get_model(self):
        return HouseListing

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(is_delete=False)

    def prepare_text(self, obj):
        return f'{obj.title} {obj.address} {obj.city} {obj.price} {obj.area} {obj.description} {obj.facilities}'

    def prepare_orientation(self, obj):
        return obj.orientation if obj.orientation else '未知'

    def prepare_deposit(self, obj):
        return obj.deposit if obj.deposit else '0'

    def prepare_landlord(self, obj):
        return obj.landlord if obj.landlord else '未知' 